using System;
using System.Data;
using System.Collections.Specialized;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Smo = Microsoft.SqlServer.Management.Smo;

namespace SqlWebAdmin.CSharp
{

	/// <summary>
	/// Summary description for SmoRoleProvider
	/// </summary>
	public class SmoRoleProvider : RoleProvider
	{
		public SmoRoleProvider()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		#region Variables

		private Smo.Server smoServer = null;
		private Smo.Login currentLogin = null;
		private HttpContext context = null;

		#endregion

		#region Provider Methods
		public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
		{
			throw new Exception("The method or operation is not implemented.");
		}
		#endregion

		#region RoleProvider Methods
		public override void AddUsersToRoles(string[] usernames, string[] roleNames)
		{
			throw new Exception("The method or operation is not implemented.");
		}

		public override string ApplicationName
		{
			get
			{
				throw new Exception("The method or operation is not implemented.");
			}
			set
			{
				throw new Exception("The method or operation is not implemented.");
			}
		}

		public override void CreateRole(string roleName)
		{
			throw new Exception("The method or operation is not implemented.");
		}

		public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
		{
			throw new Exception("The method or operation is not implemented.");
		}

		public override string[] FindUsersInRole(string roleName, string usernameToMatch)
		{
			throw new Exception("The method or operation is not implemented.");
		}

		public override string[] GetAllRoles()
		{
			ConfigureConnection();
			throw new Exception("The method or operation is not implemented.");
			Smo.ServerRoleCollection serverRoles = smoServer.Roles;
			string[] ret = new string[serverRoles.Count];

			for (int i = 0; i < serverRoles.Count; i++)
			{
				ret[i] = serverRoles[i].Name;
			}
		}

		public override string[] GetRolesForUser(string username)
		{
			ConfigureConnection();
			Smo.Login login = GetLoginFromName(username);

			string dbName = SmoHelper.GetDatabase().Name;
			string dbUserName = login.GetDatabaseUser(dbName);

			Smo.User dbUser = new Smo.User(SmoHelper.GetDatabase(), dbUserName);

			StringCollection serverRoles = dbUser.EnumRoles();

			string[] ret = new string[serverRoles.Count];
			
			for (int i = 0; i < serverRoles.Count; i++)
			{
				ret[i] = serverRoles[i];
			}
			 
			return ret;

		}

		public override string[] GetUsersInRole(string roleName)
		{
			throw new Exception("The method or operation is not implemented.");
			
		}

		public override bool IsUserInRole(string username, string roleName)
		{
			ConfigureConnection();
			Smo.Login login = GetLoginFromName(username);

			string dbName = SmoHelper.GetDatabase().Name;
			string dbUserName = login.GetDatabaseUser(dbName);

			Smo.User dbUser = new Smo.User(SmoHelper.GetDatabase(),dbUserName);

			if (login.IsMember(roleName))
				return true;
			else if (dbUser.IsMember(roleName))
				return true;
			else
				return false;
		}

		public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
		{
			throw new Exception("The method or operation is not implemented.");
		}

		public override bool RoleExists(string roleName)
		{
			throw new Exception("The method or operation is not implemented.");
		}
		#endregion

		#region Helper Methods

		private void ConfigureConnection()
		{
			if(smoServer == null)
				smoServer = SmoHelper.GetServer();
		}

		private Smo.Login GetLoginFromName(string name)
		{

			return new Smo.Login(smoServer, smoServer.ConnectionContext.Login);
		}

		#endregion
	}
}